package com.atguigu.mapReduce.outputFormat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

public class IndexRecordWriter extends RecordWriter<Text, NullWritable> {
    private  FSDataOutputStream otherOutput;
    private  FSDataOutputStream comOutput;

    public IndexRecordWriter(TaskAttemptContext job) {
        try {
            FileSystem fs = FileSystem.get(job.getConfiguration());
            comOutput = fs.create(new Path("/Users/cokerzhou/Documents/UGit/Hadoop/output/com.txt"));
            otherOutput = fs.create(new Path("/Users/cokerzhou/Documents/UGit/Hadoop/output/other.txt"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String s = key.toString();
        if(s.contains("com")){
            comOutput.writeBytes(s+"\n");
        }else {
            otherOutput.writeBytes(s+"\n");
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(comOutput);
        IOUtils.closeStream(otherOutput);
    }
}
